package com.caishi.lkx.exam.api;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.role.Role;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.exam.model.PaperRuleModel;
import com.caishi.lkx.exam.service.IExColumnConfigReService;
import com.caishi.lkx.exam.service.IPaperRuleService;
import com.caishi.lkx.user.model.UserModel;
import com.caishi.lkx.user.service.impl.UserServiceImpl;
import com.zzw.common.Result;
import com.zzw.common.Wrappers;

import com.caishi.lkx.exam.ExamResultCode;

import com.caishi.lkx.exam.model.re.ExColumnConfigReModel;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author by Janisa
 * @Description:
 * @date 2022/3/7 16:25
 */
@Tag(name = "组卷规则相关接口")
@Slf4j
@RestController
@RequestMapping("/exam/rule")
public class PaperRuleApi extends AdminBaseApi<IPaperRuleService, PaperRuleModel, String> {

    @Resource
    private IPaperRuleService service;

    @Resource
    private UserServiceImpl userService;

    @Resource
    private IExColumnConfigReService exColumnConfigReService;

    @Override
    public IPaperRuleService loadService() {
        return service;
    }

    @Override
    protected LambdaQueryWrapper<PaperRuleModel> baseWrapper(PaperRuleModel model, JSONObject otherParam) {
        return Wrappers.lambdaQuery(model.clearEmptyStringField("name", "remark"))
                .like(StringUtil.isNotBlank(model.getName()), PaperRuleModel::getName, model.getName())
                .like(StringUtil.isNotBlank(model.getRemark()), PaperRuleModel::getRemark, model.getRemark() );
    }
    @Override
    @Roles({Role.admin})
    @GetMapping("/page")
    public IPage<PaperRuleModel> page(@ModelAttribute Page<PaperRuleModel> page, @ModelAttribute PaperRuleModel model, @NotNull JSONObject otherParam) {
        LambdaQueryWrapper<PaperRuleModel> wrapper = baseWrapper(model, otherParam);
        Integer categoryId = otherParam.getInt("categoryId");
        if(categoryId != null){
            wrapper.like(PaperRuleModel::getCategories,categoryId);
        }
        wrapper.orderByDesc(PaperRuleModel::getCreatedTime);
        IPage<PaperRuleModel> data = loadService().page(page,wrapper);
        data.getRecords().parallelStream().forEach(this::setUserName);
        data.setRecords(listCover(data.getRecords(), otherParam));
        return data;
    }

    @Roles({Role.admin})
    @DeleteMapping("/deleted-list")
    public Result<String> deletedList(@RequestParam(value = "ids") List<String> ids) {
        Boolean delete = service.delete(ids);
        return Boolean.TRUE.equals(delete) ? Result.success("删除成功") : Result.fail(ExamResultCode.deletedTypeFail);
    }

    @GetMapping("/rule-by-columnId")
    @Operation(summary = "根据栏目拉取规则")
    @Parameters({
            @Parameter(name = "columnId", description = "栏目id",required = true),
    })
    public List<PaperRuleModel> getRuleForColumn(@RequestParam String columnId) {
        return exColumnConfigReService.selectList(Wrappers.<ExColumnConfigReModel>lambdaQuery()
                .eq(ExColumnConfigReModel::getColumnId, columnId)).parallelStream().map(model -> service.getById(model.getConfigId())).toList();
    }

    private void setUserName(PaperRuleModel model) {
        UserModel userDetail = userService.getUserDetail(model.getModifyBy());
        if(userDetail != null){
            String userName = userDetail.getName();
            if (userName != null) {
                model.setFinalModifyUserName(userName);
            }
        }
    }
}
